import 'package:flutter/material.dart';
import 'package:shopping_group/base/base_constant.dart';
import 'package:shopping_group/entity/dim_fileinfo_entity.dart';
import 'package:shopping_group/modle/notice/bloc/forum_bloc.dart';
import 'package:shopping_group/res/colors.dart';
import 'package:shopping_group/utils/object_util.dart';
import 'package:shopping_group/utils/photo_utils.dart';
import 'package:shopping_group/utils/widget_utils.dart';
import 'package:shopping_group/view/button_progress_view.dart';
import 'package:bot_toast/bot_toast.dart';

class ForumReleasePage extends StatefulWidget {
  ForumReleasePage({Key key}) : super(key: key);

  @override
  _ForumReleasePageState createState() => _ForumReleasePageState();
}

class _ForumReleasePageState extends State<ForumReleasePage> {
  TextEditingController _titlecontroller = TextEditingController();
  TextEditingController _controller = TextEditingController();
  List<DimFileinfoEntity> listPhoto = [];
  ForumBloc _bloc;
  @override
  void initState() {
    super.initState();
    _bloc = ForumBloc();
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: () {
        // 触摸收起键盘
        FocusScope.of(context).requestFocus(FocusNode());
      },
      child: Scaffold(
        appBar: WidgetUtils.showAppar(context, '发布'),
        body: SingleChildScrollView(
          scrollDirection: Axis.vertical,
          reverse: false,
          padding: EdgeInsets.all(0.0),
          physics: BouncingScrollPhysics(),
          child: Container(
            alignment: Alignment.centerLeft,
            decoration: BoxDecoration(
              color: Colours.title_white,
            ),
            child: Column(
              children: <Widget>[
                SizedBox(
                  height: 8,
                ),
                _showlistView(),
                SizedBox(
                  height: 8,
                ),
                ButtonProgressView.crateCupertinoView('发布',
                    horizontalPadding: 8,
                    textColor: Colours.title_white,
                    blackColor: Colours.themeColor, cupertinoCall: () {
                  String title = _titlecontroller.text;
                  if (ObjectUtil.isEmptyString(title)) {
                    BotToast.showText(text: '标题不能为空');
                    return;
                  }

                  String content = _controller.text;
                  if (ObjectUtil.isEmptyString(content)) {
                    BotToast.showText(text: '发布内容不能为空');
                    return;
                  }
                  if (ObjectUtil.isEmptyList(listPhoto) &&
                      listPhoto.length > 5) {
                    BotToast.showText(text: '图片不能超过5张');
                    return;
                  }
                  Map<String, dynamic> data = {};
                  data['id'] = '';
                  data['title'] = title;
                  data['info'] = content;
                  data['type'] = 'NT_CAN_GUI';
                  data['createUserId'] = BaseConstant.userId;

                  if (ObjectUtil.isEmptyList(listPhoto)) {
                    data['lstNoticesFile'] = listPhoto;
                  } else {
                    List<List<int>> photos = [];
                    for (var item in listPhoto) {
                      photos.add(item.imageData);
                    }
                    data['photos'] = photos;
                  }
                  _bloc.initDate.add(data);
                }),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget _showlistView() {
    // return StreamBuilder(
    //   stream: _bloc.sendData,
    //   initialData: _listEntity,
    //   builder: (BuildContext contexts, AsyncSnapshot<Object> snapshot) {
    //     Object object = snapshot.data;
    //     if (object is List) {
    //       _listEntity = object;
    //     }
    return Container(
      decoration: new BoxDecoration(
        color: Colors.white,
      ),
      child: _xunchaItem(),
    );
    //     },
    //   );
  }

  Widget _xunchaItem() {
    return Container(
        padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 8),
        child: Column(
          children: <Widget>[
            WidgetUtils.showResultTextField(_titlecontroller,
                minLines: 2, maxLength: 150, hintText: '请输入发布标题'),
            WidgetUtils.showResultTextField(_controller,
                minLines: 5, maxLength: 500, hintText: '请输入发布内容'),
            SizedBox(
              height: 4,
            ),
            _photoView(),
            SizedBox(
              height: 4,
            ),
          ],
        ));
  }

  Widget _photoView() {
    return Padding(
        padding: const EdgeInsets.symmetric(horizontal: 16),
        child: PhotoUtils.getPhotoWidget(listPhoto, context, '选择照片',
            takePhoto: false, startPhoto: (dimFileinfoVO) {
          setState(() {
            listPhoto.add(dimFileinfoVO);
          });
        }, delPhoto: (dimFileinfoVO) {
          setState(() {
            listPhoto.remove(dimFileinfoVO);
          });
        }));
  }

  @override
  void dispose() {
    _controller?.dispose();
    _titlecontroller?.dispose();
    super.dispose();
  }
}
